package com.enjoy.xbase.xui.list;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.enjoy.xbase.R;
import com.enjoy.xbase.xui.adapter.BaseRvAdapter;
import com.enjoy.xbase.xui.adapter.BaseRvViewHolder;
import com.enjoy.xbase.xui.load.BaseLoad;
import com.enjoy.xbase.xui.views.RefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：
 * 通用加载列表类数据基类
 * 介绍：
 * 1、定制加载页面通过在onInitComplete()方法中设置加载交互器完成setLoadMutual()
 */
public abstract class CommonListFragment extends Fragment {

    protected LinearLayout rootView;
    protected RefreshListView refreshListView;


    protected List outData = new ArrayList();
    private Object outParam;
    protected boolean isEnd;
    protected boolean hasData;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = (LinearLayout) inflater.inflate(R.layout.xbase_fragment_common_list_v3, container, false);
            initTopView();
            initBaseView();
            onInitBaseComplete();
        }
        return rootView;
    }
    private void initTopView(){
        View topView=onCreateTopView(rootView);
        if (topView!=null)rootView.addView(topView,0);
    }



    protected final void initBaseView() {
        refreshListView=rootView.findViewById(R.id.refresh_list);

        refreshListView.setListColumn(getColumn());
        refreshListView.setEnableRefresh(canRefresh());
        refreshListView.setEnableLoadMore(canLoadMore());
        refreshListView.setDataHelper(new RefreshListView.DataHelper() {
            @Override
            public void requestData(int page) {
                CommonListFragment.this.requestData(page);
            }

            @Override
            public int getItemViewType(int position) {
                return CommonListFragment.this.getItemViewType(position);
            }

            @Override
            public int getItemLayoutId(int viewType) {
                return CommonListFragment.this.getItemLayoutId(viewType);
            }

            @Override
            public void onBindViewHolder(BaseRvViewHolder holder, int type, int position) {
                CommonListFragment.this.onBindViewHolder(holder,position);
            }
        });
        if (autoLoadData()) {
            refreshListView.xInitAndData();
        } else {
            refreshListView.xInit();
        }
        initComplete();
    }

    /**
     * 初始化完成
     */
    public void onInitBaseComplete() {

    }

    /**
     * 描述：顶部视图
     */
    protected View onCreateTopView(ViewGroup container){
        return null;
    }

    public void setLoad(BaseLoad load) {
        refreshListView.setLoad(load);
    }

    public void setHasFooter(boolean hasFooter,int footerLayoutId) {
        refreshListView.setHasFooter(hasFooter,footerLayoutId);
    }

    /**
     * 返回列数
     */
    protected int getColumn() {
        return 1;
    }

    /**
     * 描述：是否自动刷新数据
     *
     * @return
     */
    protected boolean autoLoadData() {
        return true;
    }

    /**
     * 是否支持加载更多
     */
    protected boolean canLoadMore() {
        return false;
    }

    /**
     * 是否支持刷新
     */
    protected boolean canRefresh() {
        return false;
    }

    public RecyclerView getRecyclerView() {
        return refreshListView.getRecyclerView();
    }

    /**
     * 描述：获取适配器
     *
     * @return
     */
    public BaseRvAdapter getAdapter() {
        return refreshListView.getAdapter();
    }

    /**
     * 描述：设置数据层颜色
     */
    protected void setDataBackgroundColor(@ColorInt int color) {
        refreshListView.setBackgroundColor(color);
    }

    /**
     * 描述： 返回是否滑动到顶部
     */
    public boolean isToTop() {
        if (refreshListView != null) {
            return !refreshListView.getRecyclerView().canScrollVertically(-1);
        }
        return true;
    }

    /**
     * 描述：返回是否滑动到底部
     *
     * @return
     */
    public boolean isToBottom() {
        if (refreshListView != null) {
            return !refreshListView.getRecyclerView().canScrollVertically(1);
        }
        return true;
    }

    public void refreshData() {
        refreshListView.refreshData();
    }

    public void refreshAndLoad() {
        refreshListView.refreshAndLoad();
    }

    public void refreshAndLoading() {
        refreshListView.refreshAndLoading();
    }

    /**
     * 描述：设置外部传入数据
     *
     * @param page  页码 从0开始
     * @param data
     * @param param 携带参数将传入load展示
     */
    private synchronized void setOutData(int page, List data, Object param) {
        setOutData(page, data, false, param);
    }

    /**
     * 描述：设置外部传入数据
     *
     * @param page  页码 从0开始
     * @param data
     * @param end   是否结束
     * @param param 携带参数将传入load展示
     */
    private synchronized void setOutData(int page, List data, boolean end, Object param) {
        if (refreshListView!=null) {
            refreshListView.setRequestData(page, data,end, param);
        } else {
            hasData=true;
            if (page == 0) {
                if (data == null) {
                    this.outData = null;
                    this.outParam = param;
                } else {
                    this.outParam = param;
                    this.outData.clear();
                    this.outData.addAll(data);
                }
            } else {
                if (data != null) {
                    this.outData.addAll(data);
                }
            }
        }
    }

    private synchronized void initComplete() {
        if (hasData){
            refreshListView.setRequestData(0, outData,isEnd, outParam);
        }
    }

    protected void setRequestData(int page, List data) {
        setRequestData(page, data, null);
    }

    /**
     * 描述：设置请求后的数据
     *
     * @param page
     * @param data null-失败
     */
    protected void setRequestData(int page, List data, Object param) {
        refreshListView.setRequestData(page, data,false, param);
    }

    public <T> T getData(int position) {
        return (T) refreshListView.getData(position);
    }

    public <T> List<T> getAllData() {
        return refreshListView.getAllData();
    }

    public void removeData(Object data) {
        refreshListView.removeData(data);
    }

    public void notifyDataSetChanged() {
        refreshListView.notifyDataSetChanged();
    }

    /**
     * 请求数据
     * 请求完成后需调用setRequestData(page,data)方法
     */
    protected abstract void requestData(int page);

    public int getItemCount() {
        return refreshListView.getAllData().size();
    }

    public int getItemViewType(int position) {
        return 0;
    }

    public abstract int getItemLayoutId(int viewType);

    public abstract void onBindViewHolder(BaseRvViewHolder holder, int position);
}
